import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    isHide:false,
    loadUrl:"/Api/Task/infoPreTrailList", //加载url
    page:1,
    pagesize:10,
    list:null,
    token:null,
    status:1,
    noMoreData:false,
    loadingText:"上拉加载更多",
    loadingTextArr:["上拉加载更多","加载中...","没有更多数据了"]
  },
  //事件处理函数
  jumpDetail: function(e) {
    var listid=e.currentTarget.dataset.listid;
    var reason=e.currentTarget.dataset.reason;
    var status=e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '../detail/index?listid='+listid+"&reason="+reason+"&status="+status,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  changestatus: async function(e){
    var status=e.currentTarget.dataset.status;
    this.setData({
      status:status,
      page:1,
      list:[]
    })
    var result=await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  onShow:function(){
    console.log("onShow");
    if(this.data.isHide) this.onLoad();
    this.setData({
      isHide:false
    })
  },
  onHide:function(){
    this.setData({
      isHide:true
    })
  },
  onLoad: async function () {
    wx.showLoading({
      title:"加载中...",
      status:1,
      page:1,
      list:[]
    })
    const token = await app.getToken();
    this.setData({
      page:1,
      token:token,
      loadingText:this.data.loadingTextArr[1]
    })
    var result= await this._loadData();
    wx.hideLoading();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
  },
  _loadData:function(){
      var p = new Promise((resolve,reject)=>{
          //做一些异步操作
          console.log("res1");
          app.request(this.data.loadUrl,{token:this.data.token,page:this.data.page,pagesize:this.data.pagesize,status:this.data.status},res=>{
            console.log("res2");
            console.log(res);
            var data=res.data;
            /* var data={
              state:true,
              data:
              [
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                },
                {
                  "listid":"1",
                  "user_name":"张姗姗",
                  "user_phone":"13456749302",
                  "status":"未处理"
                }
              ]
            }; */
            if(data.state){
              resolve({state:true,list:data.data});
              //resolve({state:true,list:data.data});
            }
            else{
              resolve({state:false,msg:data.msg});          
            }
          },error=>{
            resolve({state:false,msg:"刷新失败，请重试"});
          })
      });
      return p;
  },
  onPullDownRefresh: function () {
    this.setData({
      page:1
    })
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var result=this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list,
        loadingText:list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    if(this.data.noMoreData) return;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    this.setData({
      page:this.data.page+1,
      loadingText:this.data.loadingTextArr[1]
    })
    var result=this._loadData();
    if(result.state){
      var list=this.data.list.concat(result.list);
      this.setData({
        list:list,
        loadingText:result.list.length==0?this.data.loadingTextArr[2]:this.data.loadingTextArr[0],
        noMoreData:result.list.length==0
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      this.setData({
        loadingText:this.data.loadingTextArr[0]
      })
    }
    // 隐藏加载框
    wx.hideLoading();
  }
})
